//Validate with regex and output error to defined id
function validateAndOutput(regex, input, errorToWrite, validToWrite, idToWrite) {
	var valid = (regex.test(input));	
	validToWrite = '<span class="valid">' + validToWrite + '</span>';
	errorToWrite = '<span class="error">' + errorToWrite + '</span>';
	document.getElementById(idToWrite).innerHTML = valid ? validToWrite : errorToWrite;
	return valid;
}

function validateName() {
	return validateAndOutput(/^[a-zA-Z ]{5,}$/,document.getElementById('name').value,'ERROR!','OK','nama_info');
}

function validateUname() {
	return validateAndOutput(/^[\w\.]{5,}$/, document.getElementById('uname').value,'ERROR!','OK','uname_info');
}

function validatePw() {
	return validateAndOutput(/^.{6,}$/, document.getElementById('pw').value,'ERROR!','OK','pw_info');
}

function validatePwa() {
	var pw1 = document.getElementById('pw').value;
	var pw2 = document.getElementById('pwa').value;
	if(pw2 != pw1)	document.getElementById('pwa_info').innerHTML = '<span class="error">ERROR</span>';
	else		document.getElementById('pwa_info').innerHTML = '<span class="valid">OK</span>';

        return (pw2 == pw1);
}

function validateEmail() {
	return validateAndOutput(/^\w{5,}@(\w+\.)+\w{2,}$/, document.getElementById('email').value,'ERROR!','OK','email_info');
}

function validateProfPic() {
	return validateAndOutput(/\.(jpg|jpeg|bmp|png)$/, document.getElementById('profpic').value,'ERROR!','OK','profpic_info');
}

function validateLoc() {
	return validateAndOutput(/^.{5,}$/, document.getElementById('loc').value,'ERROR!','OK','loc_info');
}

function validateBirthdate() {
	if(!validateAndOutput(/^\d{4}(-\d{2}){2}$/, document.getElementById('bd').value,'ERROR!','OK','bd_info')) return false;
	date = document.getElementById('bd').value.split('-');
	validDate = new Date(date[0], date[1], date[2]);
	valid = validDate.getFullYear() == date[0] && validDate.getMonth() == date[1] && validDate.getDate() == date[2];
	return validateAndOutput(/^.$/, valid ? 'a' : 'asdf','ERROR!','OK','bd_info');
}

function validateDateTaken() {
	if(!validateAndOutput(/^\d{4}(-\d{2}){2}$/, document.getElementById('date_taken').value,'ERROR!','OK','date_taken_info')) return false;
	date = document.getElementById('date_taken').value.split('-');
	validDate = new Date(date[0], date[1], date[2]);
	valid = validDate.getFullYear() == date[0] && validDate.getMonth() == date[1] && validDate.getDate() == date[2];
	return validateAndOutput(/^.$/, valid ? 'a' : 'asdf','ERROR!','OK','date_taken');
}

function validateDesc() {
	return validateAndOutput(/^.{5,}$/, document.getElementById('desc').value,'ERROR!','OK','desc_info');
}

function validateKeyword() {
	return validateAndOutput(/^.{3,}$/, document.getElementById('keyword').value,'ERROR!','OK','keyword_info');
}

function validatePhotoUpload() {
    var valid = 
        validateProfPic() &&
        validateDesc() && validateKeyword() &&
        validateDateTaken();

    if(!valid) alert("You've entered invalid input!");

    return valid;
}

function validateRegistration() {
    var valid =
        validateName() && validateUname() && validatePw() &&
        validatePwa() && validateEmail() &&
        validateBirthdate();
    if(!validateName()) alert("1");
    if(!validateUname()) alert("2");
    if(!validatePw()) alert("3");
    if(!validatePwa()) alert("4");
    if(!validateEmail()) alert("5");
    if(!validateBirthdate()) alert("6");

    if(!valid) alert("You've entered invalid input in registration form!");

    return valid;
}